package com.example.ying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ying.mapper.AdminMapper;
import com.example.ying.model.LoginAdmin;
import com.example.ying.model.domain.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public UserDetails loadUserByUsername(String username) {
        //根据用户名查询用户信息
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getName,username);
        Admin admin = adminMapper.selectOne(wrapper);
        //如果查询不到数据就通过抛出异常来给出提示
        if(Objects.isNull(admin)){
            throw new UsernameNotFoundException("用户名不存在！");
        }
        //TODO 根据用户查询权限信息 添加到LoginAdmin中
        List<String> premissions = new ArrayList<>();

        //封装成UserDetails对象返回
        return new LoginAdmin(admin,premissions);
    }
}

